<?php
/**
 * SmsOutbox Model
 *
 * @property Item
 */
class SmsOutbox extends AppModel {
/**
 * Display field
 *
 * @var string
 */

    var $belongsTo = array(
        'SmsMeta' => array(
            'className' => 'SmsMeta',
            'foreignKey' => 'sms_meta_id',
            'conditions' => '',
            'order' => '',
            'limit' => '',
            'dependent' => false
        ),
        'Contact' => array(
            'className' => 'Contact',
            'foreignKey' => 'contact_id',
            'conditions' => '',
            'order' => '',
            'limit' => '',
            'dependent' => false
        ),
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'order' => '',
            'limit' => '',
            'dependent' => false
        )
    );


    function getSMSTotalPending($user_id,$year_month){
         return $this->getSMSTotal($user_id,'Pending',$year_month);
    }

    function getSMSTotalDelivered($user_id,$year_month){
        return $this->getSMSTotal($user_id,'Delivered',$year_month);
    }

    function getSMSTotal($user_id,$type,$year_month){
        $data = $this->find('count', array(
            'conditions' => array(
                'SmsOutbox.user_id' => $user_id,
                'SmsOutbox.status' => $type,
                'SmsOutbox.created LIKE' => $year_month.'%',
                'SmsOutbox.deleted' => 'n'
            ),
            'recursive' => -1
        ));
        return $data;
    }

    function getSMSsatatitics($user_id,$year){
        $data = $this->find('all', array(
            'fields'=>array('created','status'),
            'conditions' => array(
                'SmsOutbox.user_id' => $user_id,
                'SmsOutbox.created LIKE' => $year.'%',
                'SmsOutbox.deleted' => 'n'
            ),
            'order'=>array('SmsOutbox.created'=>'asc'),
            'recursive' => -1
        ));

        $per_month = array();
        foreach($data as $dt){
            $month = date('F',strtotime($dt['SmsOutbox']['created']));
            $status = $dt['SmsOutbox']['status'];
            if(isset($per_month[$month])){
                $delivered = $status=='Delivered' ? 1:0;
                $pending = $status=='Pending' ? 1:0;
                $per_month[$month]['delivered'] = $per_month[$month]['delivered'] + $delivered;
                $per_month[$month]['pending'] = $per_month[$month]['pending'] + $pending;
            }
            else{
                $delivered = $status=='Delivered' ? 1:0;
                $pending = $status=='Pending' ? 1:0;
                $per_month[$month] = array(
                    'delivered'=>$delivered,
                    'pending'=>$pending,
                );
            }
        }

        $g_data = array(
            'x-axis'=>array(),
            'data'=>array(
                array('name'=>'Delivered','data'=>array()),
                array('name'=>'Pending','data'=>array())
            )
        );

        foreach($per_month as $month => $value){
            $g_data['x-axis'][] = $month;
            $g_data['data'][0]['data'][] = $value['delivered'];//Delivered
            $g_data['data'][1]['data'][] = $value['pending'];//Pending
        }

        return $g_data;
    }
   
}
